import pandas as pd
import numpy as np
from emtencent import config as cn
import json
import time
from urllib.request import urlopen, Request
import re
import datetime
import sys
import numpy as np
# 获取当天市场资金
def get_market_money_flow():
    req = Request(cn.MONEY_FLOW_MARKET_BROWSER)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    text = text.replace('var v_funds_dapan="', '').replace('";', ';').replace('^',';').replace('~',',')
    groups = text.split(';')
    datas = [item.split(',') for item in groups if len(item)>0]
    # 主力买入，主力卖出，主力净流入，散户买入，散户卖出，散户净流入，日期，时间 , dtype=[float, float, float, float, float, float, str, str]
    columns = ['ZL_IN', 'ZL_OUT', 'ZL_ACU', 'SH_IN', 'SH_OUT', 'SH_ACU', 'DATE', 'TIME']
    df = pd.DataFrame(datas, columns=columns)
    df[['ZL_IN', 'ZL_OUT', 'ZL_ACU', 'SH_IN', 'SH_OUT', 'SH_ACU']] = df[['ZL_IN', 'ZL_OUT', 'ZL_ACU', 'SH_IN', 'SH_OUT', 'SH_ACU']].astype(float)
    df['ZL_ACU_PCT'] = df['ZL_ACU'] / (df['ZL_IN'] + df['ZL_OUT'].abs())
    df['SH_ACU_PCT'] = df['SH_ACU'] / (df['SH_IN'] + df['SH_OUT'].abs())
    return df


# order,默认按流入的资金排序，传false表示按流出资金排序
def get_industry_zl_money_flow(order_in=True):
    req = Request(cn.MONEY_FLOW_INDUSTRY)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    text = (text.split(";"))[0]
    l = _handle_money_text(text)
    columns = ['industry_code', 'industry_name', 'zl_money', 'attr1', 'attr2', 'attr3', 'zl_money_pct']
    if order_in:
        return pd.DataFrame(l,columns=columns)
    else:
        return pd.DataFrame(l.reverse(),columns=columns)

def get_concept_zl_money_flow(order_in=True):
    req = Request(cn.MONEY_FLOW_CONCEPT)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    text = (text.split(";"))[0]
    l = _handle_money_text(text)
    columns = ['concept_code', 'concept_name', 'zl_money', 'attr1', 'attr2', 'attr3','zl_money_pct']
    if order_in:
        return pd.DataFrame(l, columns=columns)
    else:
        return pd.DataFrame(l.reverse(),columns=columns)

# 获取当天/5天主力增仓/减仓排序的股票，order_in表示获取增仓,param=1表示当天
def get_stokc_zl_money_flow(order_in=True,param=1):
    req = Request(cn.MONEY_FLOW_STOCK%param)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    if order_in:
        text = (text.split(";"))[0]
    else:
        text = (text.split(";"))[1]
    l = _handle_money_text(text)
    columns = ['code', 'attr1', 'attr2', 'zl_money', 'zl_money_pct', 'attr3', 'attr4', 'attr5', 'name', 'attr6', 'attr7']
    df = pd.DataFrame(l, columns=columns)
    return df

# 获取当天主力资金逆势的股票: 主力资金流入，价格下跌(7) 以及主力资金流出价格上涨(8)
def get_stock_zl_opposite_trend(order_in_price_down=True):
    is_order_in_price_down = 7 if order_in_price_down else 8
    req = Request(cn.MONEY_FLOW_ZL_OPPOSITE_TREND%is_order_in_price_down)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    text = (text.split(";"))[0]
    l = _handle_money_text(text)
    columns = ['code','attr1','attr2','zl_money','attr3','attr4','attr5','zl_pct','name','attr6','attr7','amount_pct','index']
    df = pd.DataFrame(l,columns=columns)
    return df

# 主力资金放量
def get_stock_zl_great_in():
    is_order_in_price_down = 9
    req = Request(cn.MONEY_FLOW_ZL_OPPOSITE_TREND % is_order_in_price_down)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    text = (text.split(";"))[0]
    l = _handle_money_text(text)
    columns = ['code', 'attr1', 'attr2', 'zl_money', 'attr3', 'attr4', 'attr5', 'zl_pct', 'name', 'attr6', 'attr7','amount_pct', 'index']
    df = pd.DataFrame(l, columns=columns)
    return df

# 获取大宗交易
def get_block_trade(begin=None,end=None,code=None,get_all=False,pause=0.1):
    r1 = _get_block_trade(begin=begin, end=end, code=code)
    datas = r1[1]
    p = r1[0]
    if p > 1 and get_all:
        time.sleep(pause)
        for i in range(2,p):
            try:
                r = _get_block_trade(begin=begin, end=end, code=code,p=i)
                datas.extend(r[1])
            except:
                print('Time Ourt Error With get_block_trade')
                continue
            sys.stdout.write('#' + "\b\b")
            sys.stdout.flush()
    columns = ['trade_time','code','name','price','total_money','count','buyer','seller']
    df = pd.DataFrame(datas, columns=columns)
    return df


def _get_block_trade(begin=None,end=None,p=1,code=None):
    c = code if code else ''
    req = None
    if begin and end and isinstance(begin,datetime.date) and isinstance(end,datetime.date):
        b = begin.strftime('%Y-%m-%d')
        e = end.strftime('%Y-%m-%d')
        req = Request(cn.BLOCK_TRADE_TIME_LIMIT%(b,e,p,c))
    else:
        req = Request(cn.BLOCK_TRADE%(p,c))
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK').replace('var oDzjyDatas=','').replace(';','')
    K_CUR_PAGE = 'curpage'
    K_PAGES = 'pages'
    K_NUMS = 'num'
    K_DATAS = 'datas'
    text = text.replace('_curpage','"%s"'% K_CUR_PAGE).replace('_pages','"%s"'%K_PAGES).replace('_num','"%s"'%K_NUMS).replace('_datas','"%s"'%K_DATAS).replace('_o','"o"')
    js = json.loads(text)
    return (js[K_PAGES], js[K_DATAS], js[K_NUMS],js[K_CUR_PAGE])

# 获取基金持仓
def get_fund_industry_keep(increase=True, year_=0, season_=0):

    y = time.localtime().tm_year if year_ == 0 else year_
    season = time.localtime().tm_mon//4 if season_ == 0 else season_
    select_time = '%d-%s'%(y,cn.SEASONS[season])
    js = _get_fund_industry_keep(increase=increase, date=select_time)
    datas = js['data']['data']
    if datas == None:
        select_time = js['data']['allBGQ'][0]
        if select_time == None:
            return None
        js = _get_fund_industry_keep(increase=increase, date=select_time)
        datas = js['data']['data']
    # datas = [list(sorted(item.values())) for item in datas]
    datas= [[item[k] for k in sorted(item.keys())] for item in datas]
    # 所属行业，季度，持仓基金数目，持仓总市值，增持市值(注意这个是源数据的顺序，由于字典遍历的排序导致顺序跟源顺序不一样)
    columns = ['keep_fund_total','keep_fund_count','change','industry_name','season']
    df = pd.DataFrame(datas, columns=columns)
    return df

def _get_fund_industry_keep(increase=True, date=None):
    type = 'inc' if increase else 'dec'
    if date :
        req = Request(cn.FUND_INDUSTRY_KEEP % (type, date))
        text = urlopen(req, timeout=10).read()
        text = text.decode('GBK').replace('v_jjph=', '')
        js = json.loads(text)
        return js
    return None



def _handle_money_text(text):
    text = text.replace('~',",").replace('\'','\'^')
    groups = re.compile(r'([0-9]{6}(.*?)\^)').findall(text)
    l = [item[0].replace('^','').split(',') for item in groups]
    return l


# get_industry_zl_money_flow()

# get_concept_zl_money_flow()

# get_stokc_zl_money_flow()

# get_stock_zl_opposite_trend()

# get_block_trade(get_all=False,pause=1)

# get_fund_industry_keep()

# get_market_money_flow()

pass